<template>
  <el-dialog
    :title="!dataForm.recordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="就诊时间" prop="medicalRecordTime">
      <el-input v-model="dataForm.medicalRecordTime" placeholder="就诊时间"></el-input>
    </el-form-item>
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="患者年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="患者年龄"></el-input>
    </el-form-item>
    <el-form-item label="身高" prop="height">
      <el-input v-model="dataForm.height" placeholder="身高"></el-input>
    </el-form-item>
    <el-form-item label="体重" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="体重"></el-input>
    </el-form-item>
    <el-form-item label="婚姻状况" prop="maritalStatus">
      <el-input v-model="dataForm.maritalStatus" placeholder="婚姻状况"></el-input>
    </el-form-item>
    <el-form-item label="生育状况" prop="fertilityStatus">
      <el-input v-model="dataForm.fertilityStatus" placeholder="生育状况"></el-input>
    </el-form-item>
    <el-form-item label="疾病史" prop="diseasesHistory">
      <el-input v-model="dataForm.diseasesHistory" placeholder="疾病史"></el-input>
    </el-form-item>
    <el-form-item label="肝功能" prop="liverFunction">
      <el-input v-model="dataForm.liverFunction" placeholder="肝功能"></el-input>
    </el-form-item>
    <el-form-item label="支付就诊费用" prop="medicalPrice">
      <el-input v-model="dataForm.medicalPrice" placeholder="支付就诊费用"></el-input>
    </el-form-item>
    <el-form-item label="症状" prop="symptom">
      <el-input v-model="dataForm.symptom" placeholder="症状"></el-input>
    </el-form-item>
    <el-form-item label="医师姓名" prop="doctorId">
      <el-input v-model="dataForm.doctorId" placeholder="医师姓名"></el-input>
    </el-form-item>
    <el-form-item label="医师职称" prop="gradeId">
      <el-input v-model="dataForm.gradeId" placeholder="医师职称"></el-input>
    </el-form-item>
    <el-form-item label="医院名称" prop="hospitalId">
      <el-input v-model="dataForm.hospitalId" placeholder="医院名称"></el-input>
    </el-form-item>
    <el-form-item label="诊断表外键
" prop="diagnoseId">
      <el-input v-model="dataForm.diagnoseId" placeholder="诊断表外键
"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordId: 0,
          medicalRecordTime: '',
          patientName: '',
          age: '',
          height: '',
          weight: '',
          maritalStatus: '',
          fertilityStatus: '',
          diseasesHistory: '',
          liverFunction: '',
          medicalPrice: '',
          symptom: '',
          doctorId: '',
          gradeId: '',
          hospitalId: '',
          diagnoseId: ''
        },
        dataRule: {
          medicalRecordTime: [
            { required: true, message: '就诊时间不能为空', trigger: 'blur' }
          ],
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '患者年龄不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '身高不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '体重不能为空', trigger: 'blur' }
          ],
          maritalStatus: [
            { required: true, message: '婚姻状况不能为空', trigger: 'blur' }
          ],
          fertilityStatus: [
            { required: true, message: '生育状况不能为空', trigger: 'blur' }
          ],
          diseasesHistory: [
            { required: true, message: '疾病史不能为空', trigger: 'blur' }
          ],
          liverFunction: [
            { required: true, message: '肝功能不能为空', trigger: 'blur' }
          ],
          medicalPrice: [
            { required: true, message: '支付就诊费用不能为空', trigger: 'blur' }
          ],
          symptom: [
            { required: true, message: '症状不能为空', trigger: 'blur' }
          ],
          doctorId: [
            { required: true, message: '医师姓名不能为空', trigger: 'blur' }
          ],
          gradeId: [
            { required: true, message: '医师职称不能为空', trigger: 'blur' }
          ],
          hospitalId: [
            { required: true, message: '医院名称不能为空', trigger: 'blur' }
          ],
          diagnoseId: [
            { required: true, message: '诊断表外键
不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patientmedicalrecord/info/${this.dataForm.recordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.medicalRecordTime = data.patientMedicalRecord.medicalRecordTime
                this.dataForm.patientName = data.patientMedicalRecord.patientName
                this.dataForm.age = data.patientMedicalRecord.age
                this.dataForm.height = data.patientMedicalRecord.height
                this.dataForm.weight = data.patientMedicalRecord.weight
                this.dataForm.maritalStatus = data.patientMedicalRecord.maritalStatus
                this.dataForm.fertilityStatus = data.patientMedicalRecord.fertilityStatus
                this.dataForm.diseasesHistory = data.patientMedicalRecord.diseasesHistory
                this.dataForm.liverFunction = data.patientMedicalRecord.liverFunction
                this.dataForm.medicalPrice = data.patientMedicalRecord.medicalPrice
                this.dataForm.symptom = data.patientMedicalRecord.symptom
                this.dataForm.doctorId = data.patientMedicalRecord.doctorId
                this.dataForm.gradeId = data.patientMedicalRecord.gradeId
                this.dataForm.hospitalId = data.patientMedicalRecord.hospitalId
                this.dataForm.diagnoseId = data.patientMedicalRecord.diagnoseId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patientmedicalrecord/${!this.dataForm.recordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordId': this.dataForm.recordId || undefined,
                'medicalRecordTime': this.dataForm.medicalRecordTime,
                'patientName': this.dataForm.patientName,
                'age': this.dataForm.age,
                'height': this.dataForm.height,
                'weight': this.dataForm.weight,
                'maritalStatus': this.dataForm.maritalStatus,
                'fertilityStatus': this.dataForm.fertilityStatus,
                'diseasesHistory': this.dataForm.diseasesHistory,
                'liverFunction': this.dataForm.liverFunction,
                'medicalPrice': this.dataForm.medicalPrice,
                'symptom': this.dataForm.symptom,
                'doctorId': this.dataForm.doctorId,
                'gradeId': this.dataForm.gradeId,
                'hospitalId': this.dataForm.hospitalId,
                'diagnoseId': this.dataForm.diagnoseId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
